//
//  MainFirst_MagazineList_UserHeadCell_WX.m
//  ZITHER
//
//  Created by weixin on 2018/11/12.
//  Copyright © 2018 weixn. All rights reserved.
//

#import "MainFirst_MagazineList_UserHeadCell_WX.h"

@implementation MainFirst_MagazineList_UserHeadCell_WX


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
        [self setMasonry];
    }
    return self;
}

-(void)setModel:(CoverCreateListModelSub *)model{
    _model = model;
    NSString *str = [NSString stringWithFormat:@"%@%@",RootUrlImage,model.avatar];
    
    [self.PicUser sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"ucg_img_loding"]];
    
    self.labUserName.text = model.nickName;
    
//    self.labContent.text = @"未关注我";

    
    
    if (![model.follow isEqualToString:@"true"]) {
        self.InterestBtn.backgroundColor = [UIColor whiteColor];
        self.InterestBtn.selected = YES;
    }else{
        self.InterestBtn.backgroundColor = baseColorTheme;
        self.InterestBtn.selected = NO;
    }
    
//    if ( [model.followMeStatus isEqualToString:@"0"]){
//        self.labContent.text = @"未关注我";
//    }else{
//        self.labContent.text = @"已关注我";
//    }
//
//    if ([model.followStatus isEqualToString:@"0"]) {
//        //加关注
//        self.InterestBtn.backgroundColor = [UIColor whiteColor];
//        self.InterestBtn.selected = YES;
//    }else{
//        //已关注
//        self.InterestBtn.backgroundColor = baseColorTheme;
//        self.InterestBtn.selected = NO;
//    }
    
}

-(void)setUI{
    [self addSubview:self.PicUser];
    [self addSubview:self.labUserName];
    [self addSubview:self.labContent];
    [self addSubview:self.InterestBtn];
    BaseLab *lab = [[BaseLab alloc]initializeFrame:CGRectMake(0, 0, 0, 1) text:@"TA的经典之作" font:12 textColor:[UIColor colorWithHex:@"999999"] isBold:NO numberOfLines:0];
    [self addSubview:lab];
    
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.PicUser.mas_bottom).offset(8);
        make.left.equalTo(self).offset(20);
    }];}

-(void)setMasonry{
    [self.PicUser mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(10);
        make.left.equalTo(self).offset(20);
        make.width.height.mas_equalTo(50);
    }];
    [self.labUserName mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.PicUser).offset(3);
        make.left.equalTo(self.PicUser.mas_right).offset(8);
        make.centerY.equalTo(self.PicUser).offset(0);
    }];
    [self.labContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.PicUser.mas_bottom).offset(-3);
        make.left.equalTo(self.PicUser.mas_right).offset(8);
    }];
    [self.InterestBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.PicUser.mas_centerY).offset(0);
        make.right.equalTo(self).offset(-20);
        make.width.mas_equalTo(68);
        make.height.mas_equalTo(32);
    }];
}

-(BaseImageV *)PicUser
{
    if (!_PicUser) {
        _PicUser = [[BaseImageV alloc]initializeFrame:CGRectMake(0, 0, 1, 1) contentMode:2];
        [_PicUser setCornerRadiusWithRadius:25 isMasks:YES];
        [[_PicUser addTag]subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
            NSLog(@"123");
            [self showBigImage:100];
        }];
    }
    return _PicUser;
}

-(BaseLab *)labUserName
{
    if (!_labUserName) {
        _labUserName = [[BaseLab alloc]initializeFrame:CGRectMake(0, 0, 1, 1) text:@"用户名称" font:13 textColor:nil isBold:NO numberOfLines:1];
        _labUserName.font = [UIFont fontWithName:@"PingFangSC-Medium" size:15];

    }
    return _labUserName;
}

-(BaseLab *)labContent
{
    if (!_labContent) {
        _labContent = [[BaseLab alloc]initializeFrame:CGRectMake(0, 0, 1, 1) text:@"" font:14 textColor:baseColorLight isBold:NO numberOfLines:1];
        _labContent.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:12];

        
    }
    return _labContent;
}

-(BaseBtn *)InterestBtn
{
    if (!_InterestBtn) {
        _InterestBtn = [[BaseBtn alloc]initializeFrame:CGRectMake(0, 0, 1, 1) NormalText:@"已关注" NormalTextColor:[UIColor whiteColor] SeleText:@"关注" SeleTextColor:baseColorTheme  font:12 isBold:NO];
        _InterestBtn.backgroundColor = baseColorTheme;
        [_InterestBtn setCornerRadiusWithRadius:4 isMasks:YES];
        [_InterestBtn setBorderWithColor:baseColorTheme width:1];
        
        @weakify(self);
        [[_InterestBtn rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            
            if (!kUSER_INFO_MANAGER.userIsLogin) {
                [Tools pushSinge];
                return ;
            }
            
            
            x.selected = !x.selected;
            if (x.selected) {
                //加关注
                self.InterestBtn.backgroundColor = [UIColor whiteColor];
            }else{
                //已关注
                self.InterestBtn.backgroundColor = baseColorTheme;
            }
            
            
            if (!x.selected) {
                self.InterestBtn.backgroundColor = [UIColor whiteColor];
                NSString *str = nil;
                str = self.model.userId;

//                if (self.type == 1) {
//                    str = self.model.followUserId;
//                }else{
//                    str = self.model.userId;
//                }
                [[serviceDefualt loadAddUserFollowFollowUserId:str reponseObjctClass:nil]subscribeNext:^(ResponseData * x) {
                    if (x.success) {
                        [Tools showTips:@"关注成功"];
                        self.InterestBtn.backgroundColor = baseColorTheme;
                        
                    }else{
                        if (![x.errorInfo isEqualToString:@"未登录"]) {
                            [Tools showTips:x.errorInfo];
                        }else{
//                            [Tools pushSinge];
                        }
                        self.InterestBtn.selected = ! self.InterestBtn.selected;
                        if (self.InterestBtn.selected) {
                            //加关注
                            self.InterestBtn.backgroundColor = [UIColor whiteColor];
                        }else{
                            //已关注
                            self.InterestBtn.backgroundColor = baseColorTheme;
                        }
                    }
                }];
                
                
            }else{
                self.InterestBtn.backgroundColor = baseColorTheme;
                NSString *str = nil;
                str = self.model.userId;
//                if (self.type == 1) {
//                    str = self.model.followUserId;
//                }else{
//                    str = self.model.userId;
//                }
                
                
                [[serviceDefualt loadCancelUserFollowFollowUserId:str reponseObjctClass:nil]subscribeNext:^(ResponseData * x) {
                    if (x.success) {
                        [Tools showTips:@"取消关注成功"];
                        self.InterestBtn.backgroundColor = [UIColor whiteColor];
                    }else{
                        if (![x.errorInfo isEqualToString:@"未登录"]) {
                            [Tools showTips:x.errorInfo];
                        }else{
//                            [Tools pushSinge];
                        }
                        self.InterestBtn.selected = ! self.InterestBtn.selected;
                        if (self.InterestBtn.selected) {
                            //加关注
                            self.InterestBtn.backgroundColor = [UIColor whiteColor];
                        }else{
                            //已关注
                            self.InterestBtn.backgroundColor = baseColorTheme;
                        }
                    }
                }];
                
            }
        }];
    }
    return _InterestBtn;
}


-(void)showBigImage:(NSInteger)selectIndex{
    UIImageView *imageV = self.PicUser;
    // self.model.fileList[selectIndex-100];
    
    if (!self.imagePreviewViewController) {
        self.imagePreviewViewController = [[QMUIImagePreviewViewController alloc] init];
        self.imagePreviewViewController.imagePreviewView.delegate = self;
        
        // QMUIImagePreviewViewController 对于以 window 的方式展示的情况，默认会开启手势拖拽退出预览功能。
        // 如果使用了手势拖拽，并且退出预览时需要飞到某个 rect，则需要实现这个 block，在里面自己去 exit，如果不实现这个 block，退出动画会使用 fadeOut 那种
        
    }
    @weakify(self);
    self.imagePreviewViewController.customGestureExitBlock = ^(QMUIImagePreviewViewController *aImagePreviewViewController, QMUIZoomImageView *currentZoomImageView) {
        @strongify(self);
        UIImageView *imageV1 = self.PicUser;
        
        [aImagePreviewViewController exitPreviewToRectInScreenCoordinate:[imageV1 convertRect:imageV1.frame toView:nil]];
        
    };
    self.imagePreviewViewController.imagePreviewView.currentImageIndex =  selectIndex-100;// 默认查看的图片的 index
    
    [self.imagePreviewViewController startPreviewFromRectInScreenCoordinate:[imageV convertRect:imageV.frame toView:nil] cornerRadius:imageV.layer.cornerRadius];
}


#pragma mark - <QMUIImagePreviewViewDelegate>

- (NSUInteger)numberOfImagesInImagePreviewView:(QMUIImagePreviewView *)imagePreviewView {
    return 1;
}

- (void)imagePreviewView:(QMUIImagePreviewView *)imagePreviewView renderZoomImageView:(QMUIZoomImageView *)zoomImageView atIndex:(NSUInteger)index {
    // 模拟异步加载的情况
    zoomImageView.reusedIdentifier = @(index);
    [zoomImageView showLoading];
    
    UIImageView *imageV = self.PicUser;
    [zoomImageView hideEmptyView];
    zoomImageView.image = imageV.image;
    
}

- (QMUIImagePreviewMediaType)imagePreviewView:(QMUIImagePreviewView *)imagePreviewView assetTypeAtIndex:(NSUInteger)index {
    return QMUIImagePreviewMediaTypeImage;
}

#pragma mark - <QMUIZoomImageViewDelegate>

- (void)singleTouchInZoomingImageView:(QMUIZoomImageView *)zoomImageView location:(CGPoint)location {
    //点击缩回
    self.imagePreviewViewController.customGestureExitBlock(self.imagePreviewViewController, zoomImageView);
}

@end
